var errMsgL = "";
function commaEmailValidate( emailsSeperateWithCommas )
{
    errMsgL = "";
    errMsg  = "";
	var emailAry = emailsSeperateWithCommas.split(",");

    for( var x = 0; x < emailAry.length ; x++ )
	{
        var str = emailAry[x].replace(/^\s+/,'').replace(/\s+$/,'');
        if( str.length > 0 )
        {
            errMsg = "";
            if( !ValidEMail( str ) )
            {
                 errMsgL=errMsgL + str + " , ";
            }
        }
	}
    if( errMsgL == "")
    {
	    return true;
    }
    else
    {
        if( errMsgL.length > 2 )
        {
            errMsgL=errMsgL.substring( 0, errMsgL.length - 2 );
        }
        return false;
    }
}

function validateTo()
{
    var checkCount = 0;
    var form = document.releaseSendMailForm;
    var emailUserCrtlCount = form.hiddenToUsers.length;

    if( form['toRecipientsEmailsList[0].emailUser'].value == '')
    {
        form.txtErrMsg.value = 'To cannot be blank';
        return false;
    }

    if( typeof ( emailUserCrtlCount ) == "undefined" )
    {
        if( commaEmailValidate( form['toRecipientsEmailsList[0].emailUser'].value ) == false )
        {
             form.txtErrMsg.value = "Enter valid email address : " + errMsgL;
             form['toRecipientsEmailsList[0].emailUser'].focus();
             return false;
        }
        else
        {
            form.txtErrMsg.value="";
            return true;
        }
    }
    else
    {
        var nullEmailCount = 0;
        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( form['toRecipientsEmailsList[' + i + '].emailUser'].value.length == 0 )
            {
                nullEmailCount++;
            }
        }
        if( nullEmailCount == emailUserCrtlCount )
        {
            form.txtErrMsg.value = 'To cannot be blank';
            return false;
        }

        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( commaEmailValidate ( form['toRecipientsEmailsList[' + i + '].emailUser'].value ) == false )
            {
                 form.txtErrMsg.value="Enter valid email address : " + errMsgL;
                 form['toRecipientsEmailsList[' + i + '].emailUser'].focus();
                 return false;
            }
            else
            {
                checkCount++;
            }
        }

        if ( parseInt( checkCount ) > 0 )
        {
            form.txtErrMsg.value = "";
            return true;
        }
        else
        {
            return false;
        }

    }
}

/*  //COMMENTED AS FUNCTION OVERLOADING IS NOT SUPPORTED IN JAVASCRIPT 
function validateTo( form )
{
    var checkCount = 0;
    //var form = document.releaseSendMailForm;
    var emailUserCrtlCount = form.hiddenToUsers.length;

    if( form['toRecipientsEmailsList[0].emailUser'].value == '')
    {
        form.txtErrMsg.value = 'To cannot be blank';
        return false;
    }

    if( typeof ( emailUserCrtlCount ) == "undefined" )
    {
        if( commaEmailValidate( form['toRecipientsEmailsList[0].emailUser'].value ) == false )
        {
             form.txtErrMsg.value = "Enter valid email address : " + errMsgL;
             form['toRecipientsEmailsList[0].emailUser'].focus();
             return false;
        }
        else
        {
            form.txtErrMsg.value="";
            return true;
        }
    }
    else
    {
        var nullEmailCount = 0;
        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( form['toRecipientsEmailsList[' + i + '].emailUser'].value.length == 0 )
            {
                nullEmailCount++;
            }
        }
        if( nullEmailCount == emailUserCrtlCount )
        {
            form.txtErrMsg.value = 'To cannot be blank';
            return false;
        }

        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( commaEmailValidate ( form['toRecipientsEmailsList[' + i + '].emailUser'].value ) == false )
            {
                 form.txtErrMsg.value="Enter valid email address : " + errMsgL;
                 form['toRecipientsEmailsList[' + i + '].emailUser'].focus();
                 return false;
            }
            else
            {
                checkCount++;
            }
        }

        if ( parseInt( checkCount ) > 0 )
        {
            form.txtErrMsg.value = "";
            return true;
        }
        else
        {
            return false;
        }

    }
}*/

function validateCC()
{
    var checkCount = 0;
    var form = document.releaseSendMailForm;
    var emailUserCrtlCount = form.hiddenCcUsers.length;

    if( typeof ( emailUserCrtlCount ) == "undefined" )
    {
        if( commaEmailValidate( form['ccRecipientsEmailsList[0].emailUser'].value ) == false )
        {
             form.txtErrMsg.value = "Enter valid email address : " + errMsgL;
             form['ccRecipientsEmailsList[0].emailUser'].focus();
             return false;
        }
        else
        {
            form.txtErrMsg.value="";
            return true;
        }
    }
    else
    {
        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( commaEmailValidate ( form['ccRecipientsEmailsList[' + i + '].emailUser'].value ) == false )
            {
                 form.txtErrMsg.value="Enter valid email address : " + errMsgL;
                 form['ccRecipientsEmailsList[' + i + '].emailUser'].focus();
                 return false;
            }
            else
            {
                checkCount++;
            }
        }
        if ( parseInt( checkCount ) == parseInt( emailUserCrtlCount ) )
        {
            form.txtErrMsg.value = "";
            return true;
        }
        else
        {
            return false;
        }
    }
}

/*  //COMMENTED AS FUNCTION OVERLOADING IS NOT SUPPORTED IN JAVASCRIPT
function validateCC( form )
{
    var checkCount = 0;
    //var form = document.releaseSendMailForm;
    var emailUserCrtlCount = form.hiddenCcUsers.length;

    if( typeof ( emailUserCrtlCount ) == "undefined" )
    {
        if( commaEmailValidate( form['ccRecipientsEmailsList[0].emailUser'].value ) == false )
        {
             form.txtErrMsg.value = "Enter valid email address : " + errMsgL;
             form['ccRecipientsEmailsList[0].emailUser'].focus();
             return false;
        }
        else
        {
            form.txtErrMsg.value="";
            return true;
        }
    }
    else
    {
        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( commaEmailValidate ( form['ccRecipientsEmailsList[' + i + '].emailUser'].value ) == false )
            {
                 form.txtErrMsg.value="Enter valid email address : " + errMsgL;
                 form['ccRecipientsEmailsList[' + i + '].emailUser'].focus();
                 return false;
            }
            else
            {
                checkCount++;
            }
        }
        if ( parseInt( checkCount ) == parseInt( emailUserCrtlCount ) )
        {
            form.txtErrMsg.value = "";
            return true;
        }
        else
        {
            return false;
        }
    }
}*/

function validateBCC()
{
    var checkCount = 0;
    var form = document.releaseSendMailForm;
    var emailUserCrtlCount = form.hiddenBccUsers.length;

    if( typeof ( emailUserCrtlCount ) == "undefined" )
    {
        if( commaEmailValidate( form['bccRecipientsEmailsList[0].emailUser'].value ) == false )
        {
             form.txtErrMsg.value = "Enter valid email address : " + errMsgL;
             form['bccRecipientsEmailsList[0].emailUser'].focus();
             return false;
        }
        else
        {
            form.txtErrMsg.value="";
            return true;
        }
    }
    else
    {
        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( commaEmailValidate ( form['bccRecipientsEmailsList[' + i + '].emailUser'].value ) == false )
            {
                 form.txtErrMsg.value="Enter valid email address : " + errMsgL;
                 form['bccRecipientsEmailsList[' + i + '].emailUser'].focus();
                 return false;
            }
            else
            {
                checkCount++;
            }
        }
        if ( parseInt( checkCount ) == parseInt( emailUserCrtlCount ) )
        {
            form.txtErrMsg.value = "";
            return true;
        }
        else
        {
            return false;
        }
    }
}

/*  //COMMENTED AS FUNCTION OVERLOADING IS NOT SUPPORTED IN JAVASCRIPT
function validateBCC( form )
{
    var checkCount = 0;
    //var form = document.releaseSendMailForm;
    var emailUserCrtlCount = form.hiddenBccUsers.length;

    if( typeof ( emailUserCrtlCount ) == "undefined" )
    {
        if( commaEmailValidate( form['bccRecipientsEmailsList[0].emailUser'].value ) == false )
        {
             form.txtErrMsg.value = "Enter valid email address : " + errMsgL;
             form['bccRecipientsEmailsList[0].emailUser'].focus();
             return false;
        }
        else
        {
            form.txtErrMsg.value="";
            return true;
        }
    }
    else
    {
        for ( var i = 0; i < emailUserCrtlCount; i++ )
        {
            if( commaEmailValidate ( form['bccRecipientsEmailsList[' + i + '].emailUser'].value ) == false )
            {
                 form.txtErrMsg.value="Enter valid email address : " + errMsgL;
                 form['bccRecipientsEmailsList[' + i + '].emailUser'].focus();
                 return false;
            }
            else
            {
                checkCount++;
            }
        }
        if ( parseInt( checkCount ) == parseInt( emailUserCrtlCount ) )
        {
            form.txtErrMsg.value = "";
            return true;
        }
        else
        {
            return false;
        }
    }
}*/

function validateSubject()
{
    var form = document.releaseSendMailForm;
    if( alphanumSpecialChar( form.subject.value, "Subject", "N" )==false)
    {
         form.txtErrMsg.value=errMsg;
         form.subject.focus();
         return false;
     }
     else
     {
        return true;
     }
}

/* //COMMENTED AS FUNCTION OVERLOADING IS NOT SUPPORTED IN JAVASCRIPT
function validateSubject( form )
{
    //var form = document.releaseSendMailForm;
    if( alphanumSpecialChar( form.subject.value, "Subject", "N" )==false)
    {
         form.txtErrMsg.value=errMsg;
         form.subject.focus();
         return false;
     }
     else
     {
        return true;
     }
}*/

function validateMessage()
{
    var form = document.releaseSendMailForm;
    if( parseInt( form.message.value.length ) > 1000 )
    {
         form.txtErrMsg.value="Message should not contain more than 1000 characters";
         form.message.focus();
         return false;
     }
     else
     {
        return true;
     }
}

/*  //COMMENTED AS FUNCTION OVERLOADING IS NOT SUPPORTED IN JAVASCRIPT
function validateMessage( form )
{
    //var form = document.releaseSendMailForm;
    if( parseInt( form.message.value.length ) > 1000 )
    {
         form.txtErrMsg.value="Message should not contain more than 1000 characters";
         form.message.focus();
         return false;
     }
     else
     {
        return true;
     }
}*/
